﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Micrhard.FMCG.Server.Models;
using Microhard.Permission.Interface;
using Micrhard.FMCG.Server.Interface;
using Microsoft.Practices.Unity;
using Microhard.Permission.MVCFilter;
using Micrhard.FMCG.Server.Impl;
using FluentValidation.Mvc;
using Microhard.Permission.MVCFilter.MVCFilter;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Permission.Extension;
using Microhard.WebUI.Models;

namespace Microhard.FMCG.WebUI.Areas.WrokPlan.Controllers
{
    public class DailyPlanController : MicroHardBaseController<DailyModel>
    {
        private IDaily _resp = null;
        [Dependency]
        public IDaily Category
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        [MVCFilterAtrForLogon()]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Index")]
        public ActionResult GetUserMngSubKA(AttriubteOfSelectList ctrAttr)
        {
            DailyModel model = new DailyModel();
            var user = this.GetCurrentUserInfoFromSessionByCtr();
            ISubKA isk = IoC.Resolve<ISubKA>();
            IEmployee iep = IoC.Resolve<IEmployee>();
            var emp = iep.GetByUserName(user.UserName);
            List<SubKAModel> result = null;
            if (!string.IsNullOrEmpty(emp.WeixinOpenID))
            {
                result = isk.GetKAByWeixinOpenID(emp.WeixinOpenID);
            }
            else if (!string.IsNullOrEmpty(user.UserName))
            {
                result = isk.GetKAByUserName(user.UserName);
            }
            ctrAttr.Data = result.Select(r => new SelectData
            {
                ID = r.SubID.ToString(),
                Name = r.Name
            }).ToList();
            return View("SelectList", ctrAttr);
        }

        [MVCFilterAtrForReDirect(Order = 9, RedirectActionName = "Succeed", RedirectAreaName = "WrokPlan", RedirectControllerName = "DailyPlan", Condition = "mobileAdd")]
        public override ActionResult SaveCreate([CustomizeValidator(RuleSet = "Insert")]DailyModel model, bool continueEditing, Core.DBUtility.ScopeResult scope)
        {
            return base.SaveCreate(model, continueEditing, scope);
        }

        [MVCFilterAtrForWeixinMP]
        public ActionResult Succeed()
        {
            return View();
        }
    }
}
